package cc.laop.utils;

import org.apache.commons.lang3.time.DateUtils;

import java.util.Date;
import java.util.Random;
import java.util.UUID;

public class RandomUtil {
    public static String[] chars = {"a", "b", "c", "d", "e", "f", "g", "h",
            "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u",
            "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7",
            "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
            "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X",
            "Y", "Z"};

    public static String[] nums = {"1", "2", "3", "4", "5", "6", "7", "8",
            "9", "0"};

    public static String shortUuid() {
        StringBuffer shortBuffer = new StringBuffer();
        String uuid = RandomUtil.uuid();
        for (int i = 0; i < 8; i++) {
            String str = uuid.substring(i * 4, i * 4 + 4);
            int x = Integer.parseInt(str, 16);
            shortBuffer.append(chars[(x % 62)]);
        }
        return shortBuffer.toString();
    }

    public static String randomCode() {
        return randomCode(4);
    }

    public static String randomCode(int length) {
        Random random = new Random();
        StringBuffer str = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(chars.length);
            str.append(chars[index]);
        }
        return str.toString();
    }

    public static String randomNum(int length) {
        Random random = new Random();
        StringBuffer str = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(nums.length);
            str.append(nums[index]);
        }
        return str.toString();
    }

    public static Date randomDate(int days) {
        Random random = new Random();
        int differ = random.nextInt(days);
        Date date = new Date();
        return DateUtils.addDays(date, -differ);
    }

    public static String uuid() {
        return UUID.randomUUID().toString().replace("-", "");
    }
}
